package com.xianyun.book.extension;

import com.alibaba.cola.extension.Extension;
import com.xianyun.book.common.constant.BizCode;
import com.xianyun.book.domain.gateway.SmsSupportGateway;
import com.xianyun.book.domain.gateway.SystemUserGateway;
import com.xianyun.book.dto.UserRegisterCmd;
import com.xianyun.book.extensionpoint.RegisterValidatorExtPtExtPt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import javax.annotation.Resource;

/**
 * @author macos-zyj
 */
@Slf4j
@Extension(bizId = BizCode.BIZ_ID_BOOK,useCase = BizCode.USE_CAUSE_REGISTER,scenario = BizCode.MANAGER_SCENARIO)
public class ManagerRegisterValidator implements RegisterValidatorExtPtExtPt {
    @Resource
    private SmsSupportGateway smsGateway;
    @Resource
    private SystemUserGateway userGateway;

    @Override
    public void validator(UserRegisterCmd registerCmd) {
        log.info("业务线:{},功能模块:{},行为:{}",BizCode.BIZ_ID_BOOK,"平台管理员","注册-校验");
        Assert.notNull(registerCmd.getUserName(),"用户名不能为空");
        Assert.notNull(registerCmd.getPassword(),"密码不能为空");
        Assert.notNull(registerCmd.getMobile(),"手机号不能为空");
        Assert.notNull(registerCmd.getValidCode(),"校验码不能为空");
        userGateway.validRegisterUniqueCondition(registerCmd.getUserName(),BizCode.BIZ_ID_BOOK);
        String phoneNumber = registerCmd.getMobile();
        userGateway.validRegisterUniqueCondition(phoneNumber,BizCode.BIZ_ID_BOOK);
        smsGateway.validSmsCode("DEMO",phoneNumber,registerCmd.getValidCode());
    }
}
